var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var logger = require('morgan');

// 引入暴露模块路由
var registerRouter = require('./routes/register');
var loginRouter = require('./routes/login');
// var errorRouter = require('./routes/error');
// 创建服务器
var app = express();

//模板引擎
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(logger('dev'));
// post接参
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// cookie功能开启
app.use(cookieParser());
// 资源静态化
app.use(express.static(path.join(__dirname, 'public')));
// 开启session 密钥
app.use(cookieSession({ keys: ['jdudhdss'] }));

// 使用模块化路由
app.use(registerRouter);
app.use(loginRouter);
// app.use(errorRouter);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  res.send({ code: 404, msg: 'not found' })
});

// error handler
app.use(function (err, req, res, next) {
  res.render('err',{ msg: err })
});

module.exports = app;
